package org.ytor.core.sysapi.log.api;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;
import org.ytor.common.exception.BaseException;
import org.ytor.common.model.R;
import org.ytor.common.util.Strs;
import org.ytor.common.util.bean.Beans;
import org.ytor.core.BaseController;
import org.ytor.core.sqlflow.Page;
import org.ytor.core.sqlflow.builder.support.WhereBuilder;
import org.ytor.core.sysapi.log.logic.SysLogLogic;
import org.ytor.core.sysapi.log.model.SysLog;
import org.ytor.core.sysapi.log.repository.SysLogRepository;
import org.ytor.core.sysapi.log.req.SysLogReq;
import org.ytor.core.sysapi.log.resp.SysLogResp;
import org.ytor.core.util.Pages;

import java.util.Arrays;
import java.util.List;

/**
 * created by 管理员 on 2025年5月27日 11:40:29
 * <br/>
 * SysLog表控制器
 * <br/>
 */
@RestController
@RequestMapping("/sys/sysLog")
@Tag(name = "日志表")
public class SysLogController extends BaseController<SysLog, SysLogLogic, SysLogRepository> {

    /**
     * 分页查询
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询", description = "根据id查询对应的实体数据数据")
    public R<Page<SysLogResp>> page(@ParameterObject SysLogReq sysLogdata,
                                    @RequestParam(defaultValue = "1") Long pageNo,
                                    @RequestParam(defaultValue = "10") Long pageSize) {
        WhereBuilder where = new WhereBuilder();
        Page<SysLog> page = repository.queryPage(where);
        return R.success(Pages.transPage(page, SysLog::toResp));
    }

    /**
     * 根据id查询数据
     */
    @GetMapping("/queryById")
    @Operation(summary = "根据id查询", description = "根据id查询数据")
    protected R<SysLogResp> queryById(String id) {
        SysLog sysLog = repository.queryById(id);
        return R.success(sysLog.toResp());
    }

    /**
     * 新增或编辑数据
     */
    @PostMapping("/addOrUpdate")
    @Operation(summary = "新增或编辑", description = "根据有无主键id,新增或编辑数据")
    protected R<String> addOrUpdate(@RequestBody SysLogReq sysLogdata) {
        SysLog sysLog = Beans.copyProperties(sysLogdata, SysLog.class);
        repository.insertOrUpdate(sysLog);
        return R.success(sysLogdata.getId() == null ? "新增成功" : "编辑成功");
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{ids}")
    @Operation(summary = "删除", description = "根据id删除数据")
    protected R<String> delete(@PathVariable("ids") String ids) {
        if (Strs.isEmpty(ids)) {
            throw new BaseException("ids不能为空");
        }
        List<String> idList = Arrays.asList(ids.split(","));
        repository.deleteById(idList);
        return R.success("删除成功");
    }

}